/*
 * ue web
 *
 */
; (function () {
    'use strict';
    let _camera = {
        location: {},
        rotation: {},
        FlyTo: '',
        setView: '',
        rotate: '',
        cancelFlight: ''
    }
    let _poiProp = {
        url: '',
        size: {},
        offset: {},
        id: 0
    }
    var objectXZ = function (_id) {
        this.id = _id;
        this.category = '';
        this.removeFromMap = function () { }
        this.show = function (_param) { }
        this.deleteObject = '';
        this.beginPatrol = '';
        this.stopPatrol = '';
    }

    let callBackTemp = '';
    let callBackPick = '';

    function ue4api_xz(ue4frameId) {
        let xz = document.getElementById(ue4frameId).contentWindow;

        this.camera = _camera;
        this.AddFuntion = _poiProp;
        let sendJson;
        //callBackTemp = '';
        this.camera.FlyTo = function (_x, _y, _z, _roll, _pitch, _yaw, _callBack, _time = 2) {
            sendJson = {
                namespace: 'camera',
                type: 'FlyTo',
                x: _x,
                y: _y,
                z: _z,
                roll: _roll,
                pitch: _pitch,
                yaw: _yaw,
                time: _time
            };
            callBackTemp = _callBack;
            xz.postMessage(sendJson, '*');
        };
        this.camera.setView = function (_options) {
            sendJson = {
                namespace: 'camera',
                type: 'setView',
                param: {
                    location: _options.destination,
                    rotation: _options.orientation
                }
            };
            xz.postMessage(sendJson, '*');
        }
        this.camera.rotate = function (_time, _degrees, _callBack) {
            sendJson = {
                namespace: 'camera',
                type: 'rotate',
                param: {
                    time: _time,
                    degrees: _degrees
                }
            };
            callBackTemp = _callBack;
            xz.postMessage(sendJson, '*');
        }
        this.camera.cancelFlight = function () {
            sendJson = {
                namespace: 'camera',
                type: 'cancelFlight',
                param: ''
            };
            xz.postMessage(sendJson, '*');
        }
        //
        this.environment = {
            ShowEffect: function (_param) {
                //澶滄櫄
                if (_param === 'morning' || _param === 'noon'
                    || _param === 'evening' || _param === 'night') {
                    sendJson = {
                        namespace: 'environment',
                        type: 'SetTime',
                        param: _param
                    };
                } else if (_param === 'rain' || _param === 'snow'
                    || _param === 'cloud' || _param === 'sun') {
                    sendJson = {
                        namespace: 'environment',
                        type: 'SetWeather',
                        param: _param
                    };
                }
                else {
                    sendJson = {
                        namespace: 'environment',
                        type: 'ShowEffect',
                        effectName: _param,
                        param: true
                    };
                }
                xz.postMessage(sendJson, '*');
            },
            DisableEffect: function (_param) {
                //鍙栨秷澶滄櫄灏辨槸涓崍
                //涓轰簡鎶婃毚闇叉帴鍙ｅ拰cesium涓€鑷�
                if (_param === 'night' || _param === 'morning'
                    || _param === 'evening') {
                    sendJson = {
                        namespace: 'environment',
                        type: 'SetTime',
                        param: 'noon'
                    };
                }
                //鍙栨秷闆ㄩ洩澶╂皵灏辨槸鏅村ぉ
                else if (_param === 'rain' || _param === 'snow'
                    || _param === 'cloud') {
                    sendJson = {
                        namespace: 'environment',
                        type: 'SetWeather',
                        param: 'sun'
                    };
                }
                else {
                    sendJson = {
                        namespace: 'environment',
                        type: 'ShowEffect',
                        effectName: _param,
                        param: false
                    };
                }
                xz.postMessage(sendJson, '*');
            }
        };
        //
        this.measure = {
            PickPosition: function (_callBack) {
                let sendJson = {
                    namespace: 'measure',
                    type: 'PickPosition'
                };
                callBackPick = _callBack;
                xz.postMessage(sendJson, '*');
            },
            PickObject: function (_callBack) {
                let sendJson = {
                    namespace: 'measure',
                    type: 'PickObject'
                };
                callBackPick = _callBack;
                xz.postMessage(sendJson, '*');
            }

        };
        //
        this.ObjectFactory = {
            CreateBillboard: function (_param) {
                let sendJson = {
                    namespace: 'ObjectFactory',
                    type: 'CreateBillboard',
                    param: _param
                };
                var obj = new objectXZ(_param.id);
                obj.category = 'billboard';
                obj.removeFromMap = function () {
                    let sendJson = {
                        namespace: 'ObjectFactory',
                        type: 'RemoveBillboard',
                        param: this.id
                    }
                    xz.postMessage(sendJson, '*');
                }
                obj.show = function (_param) {
                    let sendJson;
                    if (_param) {
                        sendJson = {
                            namespace: 'ObjectFactory',
                            type: 'ShowBillboard',
                            param: obj.id
                        };
                    } else {
                        sendJson = {
                            namespace: 'ObjectFactory',
                            type: 'HideBillboard',
                            param: obj.id
                        };

                    }
                    xz.postMessage(sendJson, '*');
                }
                xz.postMessage(sendJson, '*');
                return obj;
            },
            CreateLabel: function (_param) {
                let sendJson = {
                    namespace: 'ObjectFactory',
                    type: 'CreateLabel',
                    param: _param
                };
                var obj = new objectXZ(_param.id);
                obj.category = 'label';
                obj.removeFromMap = function () {
                    let sendJson = {
                        namespace: 'ObjectFactory',
                        type: 'RemoveLabel',
                        param: this.id
                    }
                    xz.postMessage(sendJson, '*');
                }
                obj.show = function (_param) {
                    let sendJson;
                    if (_param) {
                        sendJson = {
                            namespace: 'ObjectFactory',
                            type: 'ShowLabel',
                            param: obj.id
                        };
                    } else {
                        sendJson = {
                            namespace: 'ObjectFactory',
                            type: 'HideLabel',
                            param: obj.id
                        };

                    }
                    xz.postMessage(sendJson, '*');
                }
                //
                xz.postMessage(sendJson, '*');
                return obj;
            },
            //
            CreateHeatmap: function (_param) {
                let sendJson = {
                    namespace: 'ObjectFactory',
                    type: 'CreateHeatmap',
                    param: _param
                };
                var obj = new objectXZ(_param.id);
                obj.removeFromMap = function () {
                    let sendJson = {
                        namespace: 'ObjectFactory',
                        type: 'RemoveHeatmap',
                        param: this.id
                    }
                    xz.postMessage(sendJson, '*');
                }
                obj.show = function (_param) {
                    let sendJson;
                    if (_param) {
                        sendJson = {
                            namespace: 'ObjectFactory',
                            type: 'ShowHeatmap',
                            param: obj.id
                        };
                    } else {
                        sendJson = {
                            namespace: 'ObjectFactory',
                            type: 'HideHeatmap',
                            param: obj.id
                        };

                    }
                    xz.postMessage(sendJson, '*');
                }
                xz.postMessage(sendJson, '*');
                return obj;
            },
            //
            CreateGrid: function (_param) {
                let sendJson = {
                    namespace: 'ObjectFactory',
                    type: 'CreateGrid',
                    param: _param
                };
                var obj = new objectXZ(_param.id);
                obj.removeFromMap = function () {
                    let sendJson = {
                        namespace: 'ObjectFactory',
                        type: 'RemoveGrid',
                        param: this.id
                    }
                    xz.postMessage(sendJson, '*');
                }
                obj.show = function (_param) {
                    let sendJson;
                    if (_param) {
                        sendJson = {
                            namespace: 'ObjectFactory',
                            type: 'ShowGrid',
                            param: obj.id
                        };
                    } else {
                        sendJson = {
                            namespace: 'ObjectFactory',
                            type: 'HideGrid',
                            param: obj.id
                        };

                    }
                    xz.postMessage(sendJson, '*');
                }
                xz.postMessage(sendJson, '*');
                return obj;
            },
            CreatePolyline: function (_param) {
                let sendJson = {
                    namespace: 'ObjectFactory',
                    type: 'AddPath',
                    param: _param
                };
                var obj = new objectXZ(_param.id);
                obj.removeFromMap = function () {
                    let sendJson = {
                        namespace: 'ObjectFactory',
                        type: 'RemovePath',
                        param: this.id
                    }
                    xz.postMessage(sendJson, '*');
                }
                obj.show = function (_param) {
                    let sendJson;
                    if (_param) {
                        sendJson = {
                            namespace: 'ObjectFactory',
                            type: 'ShowPath',
                            param: obj.id
                        };
                    } else {
                        sendJson = {
                            namespace: 'ObjectFactory',
                            type: 'HidePath',
                            param: obj.id
                        };

                    }
                    xz.postMessage(sendJson, '*');
                }
                xz.postMessage(sendJson, '*');
                return obj;
            }
        };
        //
        this.factory = {
            //
            CallEvent: function (_eventName, _param) {
                let sendJson = {
                    namespace: 'factory',
                    type: 'callEvent',
                    eventName: _eventName,
                    param: _param
                };
                xz.postMessage(sendJson, '*');
            },
            //
            FlyToCity: function () {
                let sendJson = {
                    namespace: 'factory',
                    type: 'FlyToCity',
                    param: { rotation: new Rotator(0, -25, 155) }
                };
                xz.postMessage(sendJson, '*');
            },
            //
            ReStartWorld: function () {
                let sendJson = {
                    namespace: 'factory',
                    type: 'ReStartWorld'
                };
                xz.postMessage(sendJson, '*');
            },
            //
            CreatePatrolLine: function (_param) {
                let sendJson = {
                    namespace: 'factory',
                    type: 'CreatePatrolLine',
                    param: _param
                };
                var obj = new objectXZ(_param.id);
                obj.removeFromMap = function () {
                    let sendJson = {
                        namespace: 'factory',
                        type: 'RemovePatrol',
                        param: this.id
                    }
                    xz.postMessage(sendJson, '*');
                }
                obj.beginPatrol = function (_param) {
                    let sendJson = {
                        namespace: 'factory',
                        type: 'BeginPatrol',
                        param: {
                            id: obj.id,
                            obj: _param.id,
                            category: obj.category
                        }
                    };

                    xz.postMessage(sendJson, '*');
                }
                obj.stopPatrol = function (_param) {
                    let sendJson = {
                        namespace: 'factory',
                        type: 'StopPatrol',
                        param: {
                            id: obj.id,
                            obj: _param.id,
                            category: obj.category
                        }
                    };

                    xz.postMessage(sendJson, '*');
                }
                xz.postMessage(sendJson, '*');
                return obj;
            }

        }
        //
        this.View = {
            //涓夌淮鏌辩姸鍥�
            CreateStatisticCylinder: function (option) {
                let sendJson = {
                    namespace: 'View',
                    type: 'Create3DBar',
                    param: option
                };
                var obj = new objectXZ(option.id);
                obj.removeFromMap = function () {
                    let sendJson = {
                        namespace: 'View',
                        type: 'Remove3DBar',
                        param: this.id
                    }
                    xz.postMessage(sendJson, '*');
                }
                obj.show = function (_param) {
                    let sendJson;
                    if (_param) {
                        sendJson = {
                            namespace: 'View',
                            type: 'Show3DBar',
                            param: obj.id
                        };
                    } else {
                        sendJson = {
                            namespace: 'View',
                            type: 'Hide3DBar',
                            param: obj.id
                        };

                    }
                    xz.postMessage(sendJson, '*');
                }
                xz.postMessage(sendJson, '*');
                return obj;
            }
        }
        //
        this.SelectTool = {
            select: function (_callBack) {
                let sendJson = {
                    namespace: 'SelectTool',
                    type: 'select',
                    param: true
                };
                callBackTemp = _callBack;
                xz.postMessage(sendJson, '*');
            },
            disSelect: function () {
                let sendJson = {
                    namespace: 'SelectTool',
                    type: 'select',
                    param: false
                };
                xz.postMessage(sendJson, '*');
            }
        }
        //
        this.analysis = {
            getDistanceHorizontal: function (_callBack) {
                var randomId = new Date().getTime().toString();
                let sendJson = {
                    namespace: 'analysis',
                    type: 'AddMeasure',
                    param: randomId
                };
                var obj = new objectXZ(randomId);
                obj.deleteObject = function () {
                    sendJson = {
                        namespace: 'analysis',
                        type: 'RemoveMeasure',
                        param: obj.id
                    };
                    xz.postMessage(sendJson, '*');
                }
                callBackTemp = _callBack;
                xz.postMessage(sendJson, '*');
                return obj;
            },
            getPlaneArea: function (_callBack) {
                var randomId = new Date().getTime().toString();
                let sendJson = {
                    namespace: 'analysis',
                    type: 'AddPlaneArea',
                    param: randomId
                };
                var obj = new objectXZ(randomId);
                obj.deleteObject = function () {
                    sendJson = {
                        namespace: 'analysis',
                        type: 'RemovePlaneArea',
                        param: obj.id
                    };
                    xz.postMessage(sendJson, '*');
                }
                callBackTemp = _callBack;
                xz.postMessage(sendJson, '*');
                return obj;
            },
            getMeasureHeight: function (_callBack) {
                var randomId = new Date().getTime().toString();
                let sendJson = {
                    namespace: 'analysis',
                    type: 'AddMeasureHeight',
                    param: randomId
                };
                var obj = new objectXZ(randomId);
                obj.deleteObject = function () {
                    sendJson = {
                        namespace: 'analysis',
                        type: 'RemoveMeasureHeight',
                        param: obj.id
                    };
                    xz.postMessage(sendJson, '*');
                }
                callBackTemp = _callBack;
                xz.postMessage(sendJson, '*');
                return obj;
            }
        }

        this.AddFuntion.alertWindow = function (param) {
            sendJson = {
                namespace: 'AddFuntion',
                type: 'alertWindow',
                param: {
                    id: param.id,
                    size: param.size,
                    url: param.url,
                    offset: param.offset
                }

            };
            xz.postMessage(sendJson, '*');
        }
    }
    addEventListener('message', e => {

        if (e.data.callBack === "CameraInfo") {
            _camera.location = e.data.camera.location;
            _camera.rotation = e.data.camera.rotation;

            if (document.getElementById('cameraInfo') !== null) {
                document.getElementById('cameraInfo').innerHTML = JSON.stringify(e.data.camera.location);
            }
            if (document.getElementById('pointer') !== null) {
                document.getElementById('pointer').style.transform = "rotate(" + _camera.rotation.yaw + "deg)";
            }
        }

        if (e.data.callBack === "CallBackOneParam") {

            if (e.data.customStr === 'pickWidget' && callBackPick !== '') {
                callBackPick(e.data.info);
            } else if (callBackTemp !== '') {

                callBackTemp(e.data.info);
            }
        } else if (e.data.callBack === "CallBackNoParam") {
            if (e.data.customStr === "connected" && typeof mainPage === 'function') {
                mainPage();
            }
            else if (e.data.customStr === "wsDisConnect" && typeof closedPage === 'function') {
                closedPage();
            }
            else if (callBackTemp !== '') {

                callBackTemp();
            }
        }
    });

    var Vector3 = function (_x = 0, _y = 0, _z = 0) {
        return {
            x: _x,
            y: _y,
            z: _z
        };
    }

    var Vector2 = function (_x = 0, _y = 0) {
        return {
            x: _x,
            y: _y
        };
    }

    var Rotator = function (_roll = 0, _pitch = 0, _yaw = 0) {
        return {
            roll: _roll,
            pitch: _pitch,
            yaw: _yaw
        };
    }

    var Color = function (_R = 1, _G = 1, _B = 1, _A = 1) {
        return {
            r: _R,
            g: _G,
            b: _B,
            a: _A
        }
    }

    var DefaultColor = {
        red: { r: 1, g: 0, b: 0, a: 1 },
        green: { r: 0, g: 1, b: 0, a: 1 },
        blue: { r: 0, g: 0, b: 1, a: 1 },
        yellow: { r: 1, g: 1, b: 0, a: 1 },
        purple: { r: 1, g: 0, b: 1, a: 1 },
        cyan: { r: 0, g: 1, b: 1, a: 1 },
        black: { r: 0, g: 0, b: 0, a: 1 },
        white: { r: 1, g: 1, b: 1, a: 1 },
        gray: { r: 0.5, g: 0.5, b: 0.5, a: 1 }
    }

    var bounds = function (w = -10000, e = 10000, s = 10000, n = -10000) {
        return {
            west: w,
            east: e,
            south: s,
            north: n
        }
    }
    if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) {

        // AMD. Register as an anonymous module.
        define(function () {
            return ue4api_xz;
        });
    } else if (typeof module !== 'undefined' && module.exports) {
        module.exports = ue4api_xz;
        module.exports.ue4api_xz = ue4api_xz;
    } else {
        window.ue4api_xz = ue4api_xz;
    }
}());